package com.ruoyi.know.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.know.domain.Notes;
import com.ruoyi.know.mapper.NotesMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.know.mapper.UserNoteFavoritesMapper;
import com.ruoyi.know.domain.UserNoteFavorites;
import com.ruoyi.know.service.IUserNoteFavoritesService;

/**
 * 用户笔记收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-11
 */
@Service
public class UserNoteFavoritesServiceImpl implements IUserNoteFavoritesService
{
    @Autowired
    private UserNoteFavoritesMapper userNoteFavoritesMapper;
    @Autowired
    private NotesMapper notesMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户笔记收藏
     *
     * @param favoriteId 用户笔记收藏主键
     * @return 用户笔记收藏
     */
    @Override
    public UserNoteFavorites selectUserNoteFavoritesByFavoriteId(Long favoriteId)
    {
        return userNoteFavoritesMapper.selectUserNoteFavoritesByFavoriteId(favoriteId);
    }

    /**
     * 查询用户笔记收藏列表
     *
     * @param userNoteFavorites 用户笔记收藏
     * @return 用户笔记收藏
     */
    @Override
    public List<UserNoteFavorites> selectUserNoteFavoritesList(UserNoteFavorites userNoteFavorites)
    {
        List<UserNoteFavorites> userNoteFavorites1 = userNoteFavoritesMapper.selectUserNoteFavoritesList(userNoteFavorites);
        for (UserNoteFavorites userNoteFavorites2 : userNoteFavorites1){
            Long noteId = userNoteFavorites2.getNoteId();
            //笔记
            Notes notes = notesMapper.selectNotesByNoteId(noteId);
            userNoteFavorites2.setNotes(notes);

            //收藏人名称
            SysUser user = sysUserMapper.selectUserById(userNoteFavorites2.getUserId());
            if(user != null){
                userNoteFavorites2.setUserName(user.getNickName());
            }else {
                userNoteFavorites2.setUserName(null);
            }

            //所属用户
            Long userId = notes.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            userNoteFavorites2.setSysUser(sysUser);
        }
        return userNoteFavorites1;
    }

    /**
     * 新增用户笔记收藏
     *
     * @param userNoteFavorites 用户笔记收藏
     * @return 结果
     */
    @Override
    public int insertUserNoteFavorites(UserNoteFavorites userNoteFavorites)
    {
        return userNoteFavoritesMapper.insertUserNoteFavorites(userNoteFavorites);
    }

    /**
     * 修改用户笔记收藏
     *
     * @param userNoteFavorites 用户笔记收藏
     * @return 结果
     */
    @Override
    public int updateUserNoteFavorites(UserNoteFavorites userNoteFavorites)
    {
        return userNoteFavoritesMapper.updateUserNoteFavorites(userNoteFavorites);
    }

    /**
     * 批量删除用户笔记收藏
     *
     * @param favoriteIds 需要删除的用户笔记收藏主键
     * @return 结果
     */
    @Override
    public int deleteUserNoteFavoritesByFavoriteIds(Long[] favoriteIds)
    {
        return userNoteFavoritesMapper.deleteUserNoteFavoritesByFavoriteIds(favoriteIds);
    }

    @Override
    public int cancelUserNoteFavoritesByFavoriteIds(Notes notes) {
        return userNoteFavoritesMapper.cancelUserNoteFavoritesByFavoriteIds(notes);
    }

    /**
     * 删除用户笔记收藏信息
     *
     * @param favoriteId 用户笔记收藏主键
     * @return 结果
     */
    @Override
    public int deleteUserNoteFavoritesByFavoriteId(Long favoriteId)
    {
        return userNoteFavoritesMapper.deleteUserNoteFavoritesByFavoriteId(favoriteId);
    }

    @Override
    public int getCountByFavorites(UserNoteFavorites userNoteFavorites) {
        return userNoteFavoritesMapper.getCountByFavorites(userNoteFavorites);
    }
}
